Expose observable as @Output() parameter in future safe way · Issue #23435 · angular/angular · GitHub

您所在的位置:网站首页 angular @output Expose observable as @Output() parameter in future safe way · Issue #23435 · angular/angular · GitHub

Expose observable as @Output() parameter in future safe way · Issue #23435 · angular/angular · GitHub

#Expose observable as @Output() parameter in future safe way · Issue #23435 · angular/angular · GitHub| 来源: 网络整理| 查看: 265

My current use-case for something like this is to allow Angular to manage my subscription lifetime.

Given the following template throughout for clarity (although I understand EventEmitter isn't currently guaranteed to extend Subject, and I should use .emit instead):

Only in the simplest of use-cases do I get the benefit of the observable pattern:

export class MyComponent { @Output() selected: EventEmitter = new EventEmitter(); }

However, as soon as I want to filter/map it (e.g. mouse events), or combine it with another observable (e.g. data source or correlated component), etc., I lose the benefit of the observable pattern, where the external subscribers manage the lifetime. Now I have to create an intermediate Subject and force a lifetime independent of consumers:

export class MyComponent { @Output() selected: EventEmitter = new EventEmitter(); ngOnInit() { this.subscriptions.add(this.filteredUpdates().subscribe(m => this.selected.next(m))); } ngOnDestroy() { this.subscriptions.unsubscribe(); } filteredUpdates() { return updates.pipe(filter(u => u !== null)); } updates: Subject = new Subject(); }

To address most of my need here, it would probably work if I could initialize an EventEmitter with another observable as @bygrace1986 mentioned. Maybe an extra line of code here or there, but at least I wouldn't be managing more component-related lifetimes.

export class MyComponent { @Output() selected: EventEmitter; constructor() { this.selected = new EventEmitter(filteredUpdates()); } filteredUpdates() { return updates.pipe(filter(u => u !== null)); } updates: Subject = new Subject(); }

It would also be helpful if I could just assign the cold observable product directly to the @Output parameter as mentioned by @pauldraper, but that seems like a nest of possible problems.

Anyway, I guess this is just restating the same problem in another way.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3